package com.dianping.cat.consumer.heartbeat.model.transform;

import com.dianping.cat.consumer.heartbeat.model.IVisitor;
import com.dianping.cat.consumer.heartbeat.model.entity.Detail;
import com.dianping.cat.consumer.heartbeat.model.entity.Disk;
import com.dianping.cat.consumer.heartbeat.model.entity.Extension;
import com.dianping.cat.consumer.heartbeat.model.entity.HeartbeatReport;
import com.dianping.cat.consumer.heartbeat.model.entity.Machine;
import com.dianping.cat.consumer.heartbeat.model.entity.Period;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;



public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
/*  30 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(HeartbeatReport heartbeatReport) {
/*  34 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(heartbeatReport, out);
/*  37 */     return out.toByteArray();
  }


/*  41 */   public static void build(HeartbeatReport heartbeatReport, OutputStream out) { heartbeatReport.accept(new DefaultNativeBuilder(out)); }



  public void visitDetail(Detail detail) {
/*  46 */     if (detail.getId() != null) {
/*  47 */       writeTag(1, 1);
/*  48 */       writeString(detail.getId());
    }

/*  51 */     writeTag(2, 0);
/*  52 */     writeDouble(detail.getValue());

/*  54 */     writeTag(63, 3);
  }


  public void visitDisk(Disk disk) {
/*  59 */     if (disk.getPath() != null) {
/*  60 */       writeTag(1, 1);
/*  61 */       writeString(disk.getPath());
    }

/*  64 */     writeTag(2, 0);
/*  65 */     writeLong(disk.getTotal());

/*  67 */     writeTag(3, 0);
/*  68 */     writeLong(disk.getFree());

/*  70 */     writeTag(4, 0);
/*  71 */     writeLong(disk.getUsable());

/*  73 */     writeTag(63, 3);
  }


  public void visitExtension(Extension extension) {
    if (extension.getId() != null) {
/*  79 */       writeTag(1, 1);
/*  80 */       writeString(extension.getId());
    }

/*  83 */     if (!extension.getDetails().isEmpty()) {
/*  84 */       writeTag(33, 2);
/*  85 */       writeInt(extension.getDetails().size());

/*  87 */       for (Detail detail : extension.getDetails().values()) {
/*  88 */         detail.accept(this.m_visitor);
      }
    }

/*  92 */     writeTag(63, 3);
  }


  public void visitHeartbeatReport(HeartbeatReport heartbeatReport) {
/*  97 */     writeTag(63, 0);

/*  99 */     if (heartbeatReport.getDomain() != null) {
/* 100 */       writeTag(1, 1);
/* 101 */       writeString(heartbeatReport.getDomain());
    }

/* 104 */     if (heartbeatReport.getStartTime() != null) {
/* 105 */       writeTag(2, 1);
/* 106 */       writeDate(heartbeatReport.getStartTime());
    }

/* 109 */     if (heartbeatReport.getEndTime() != null) {
/* 110 */       writeTag(3, 1);
/* 111 */       writeDate(heartbeatReport.getEndTime());
    }

/* 114 */     if (heartbeatReport.getDomainNames() != null) {
/* 115 */       writeTag(4, 2);
/* 116 */       writeInt(heartbeatReport.getDomainNames().size());

/* 118 */       for (String domain : heartbeatReport.getDomainNames()) {
/* 119 */         writeString(domain);
      }
    }

/* 123 */     if (heartbeatReport.getIps() != null) {
/* 124 */       writeTag(5, 2);
/* 125 */       writeInt(heartbeatReport.getIps().size());

/* 127 */       for (String ip : heartbeatReport.getIps()) {
/* 128 */         writeString(ip);
      }
    }

/* 132 */     if (!heartbeatReport.getMachines().isEmpty()) {
/* 133 */       writeTag(33, 2);
/* 134 */       writeInt(heartbeatReport.getMachines().size());

/* 136 */       for (Machine machine : heartbeatReport.getMachines().values()) {
/* 137 */         machine.accept(this.m_visitor);
      }
    }

/* 141 */     writeTag(63, 3);
  }


  public void visitMachine(Machine machine) {
/* 146 */     if (machine.getIp() != null) {
/* 147 */       writeTag(1, 1);
/* 148 */       writeString(machine.getIp());
    }

/* 151 */     if (machine.getClasspath() != null) {
/* 152 */       writeTag(2, 1);
/* 153 */       writeString(machine.getClasspath());
    }

/* 156 */     if (!machine.getPeriods().isEmpty()) {
/* 157 */       writeTag(33, 2);
/* 158 */       writeInt(machine.getPeriods().size());

/* 160 */       for (Period period : machine.getPeriods()) {
/* 161 */         period.accept(this.m_visitor);
      }
    }

/* 165 */     writeTag(63, 3);
  }


  public void visitPeriod(Period period) {
/* 170 */     writeTag(1, 0);
/* 171 */     writeInt(period.getMinute());

/* 173 */     writeTag(2, 0);
/* 174 */     writeInt(period.getThreadCount());

/* 176 */     writeTag(3, 0);
/* 177 */     writeInt(period.getDaemonCount());

/* 179 */     writeTag(4, 0);
/* 180 */     writeInt(period.getTotalStartedCount());

/* 182 */     writeTag(5, 0);
/* 183 */     writeInt(period.getCatThreadCount());

/* 185 */     writeTag(6, 0);
/* 186 */     writeInt(period.getPigeonThreadCount());

/* 188 */     writeTag(7, 0);
/* 189 */     writeInt(period.getHttpThreadCount());

/* 191 */     writeTag(8, 0);
/* 192 */     writeLong(period.getNewGcCount());

/* 194 */     writeTag(9, 0);
/* 195 */     writeLong(period.getOldGcCount());

/* 197 */     writeTag(10, 0);
/* 198 */     writeLong(period.getMemoryFree());

/* 200 */     writeTag(11, 0);
/* 201 */     writeLong(period.getHeapUsage());

/* 203 */     writeTag(12, 0);
/* 204 */     writeLong(period.getNoneHeapUsage());

/* 206 */     writeTag(13, 0);
/* 207 */     writeDouble(period.getSystemLoadAverage());

/* 209 */     writeTag(14, 0);
/* 210 */     writeLong(period.getCatMessageProduced());

/* 212 */     writeTag(15, 0);
/* 213 */     writeLong(period.getCatMessageOverflow());

/* 215 */     writeTag(16, 0);
/* 216 */     writeDouble(period.getCatMessageSize());

/* 218 */     if (!period.getDisks().isEmpty()) {
/* 219 */       writeTag(33, 2);
/* 220 */       writeInt(period.getDisks().size());

/* 222 */       for (Disk disk : period.getDisks()) {
/* 223 */         disk.accept(this.m_visitor);
      }
    }

/* 227 */     if (!period.getExtensions().isEmpty()) {
/* 228 */       writeTag(34, 2);
/* 229 */       writeInt(period.getExtensions().size());

/* 231 */       for (Extension extension : period.getExtensions().values()) {
/* 232 */         extension.accept(this.m_visitor);
      }
    }

/* 236 */     if (!period.getDynamicAttributes().isEmpty()) {
/* 237 */       writeTag(63, 2);
/* 238 */       writeInt(period.getDynamicAttributes().size());

/* 240 */       for (Map.Entry<String, String> dynamicAttribute : period.getDynamicAttributes().entrySet()) {
/* 241 */         writeString((String)dynamicAttribute.getKey());
/* 242 */         writeString((String)dynamicAttribute.getValue());
      }
    }

/* 246 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 251 */       writeVarint(value.getTime());
/* 252 */     } catch (Exception e) {
/* 253 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 259 */       writeVarint(Double.doubleToLongBits(value));
/* 260 */     } catch (Exception e) {
/* 261 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 267 */       writeVarint(value & 0xFFFFFFFFL);
/* 268 */     } catch (Exception e) {
/* 269 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 275 */       writeVarint(value);
/* 276 */     } catch (Exception e) {
/* 277 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 283 */       this.m_out.writeUTF(value);
/* 284 */     } catch (IOException e) {
/* 285 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 291 */       this.m_out.writeByte((field << 2) + type);
/* 292 */     } catch (IOException e) {
/* 293 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 299 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 300 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 303 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 304 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/heartbeat/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */